package cz.af.portletExams.subjectExamsOverview;

import cz.af.portletExams.backend.ExamsOverViewBackend;
import cz.af.portletExams.model.Exam;
import cz.af.portletExams.model.ExamOverviewUser;
import cz.af.portletExams.model.Subject;
import cz.af.portletExams.subjectExamsOverview.dto.DtoCreator;
import cz.af.portletExams.subjectExamsOverview.dto.ExamForSubjectExamsView;
import cz.af.portletExams.util.AFPortletUtil;
import cz.af.portletExams.util.SessionConstants;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.bind.annotation.EventMapping;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.EventRequest;
import javax.portlet.PortletSession;
import java.util.List;

@Controller
@RequestMapping("VIEW")
public class SubjectViewController {

    private static final Logger log = Logger.getLogger(SubjectViewController.class);

    @Autowired
    private ExamsOverViewBackend backend;

    @Autowired
    private ResourceBundleMessageSource messages;

    @RequestMapping
    private String renderWithSubject(Model model, PortletSession portletSession) {
        ExamOverviewUser user = (ExamOverviewUser) portletSession.getAttribute(SessionConstants.USER, PortletSession.APPLICATION_SCOPE);
        if (user == null) {
            log.info("renderWithoutUser");
            return "subjectExamsOverview/viewWithoutUser";
        } else {
            final String subjectId = (String) portletSession.getAttribute(SessionConstants.SUBJECT_DETAIL);
            final String username = user.getUsername();
            if (subjectId == null) {
                log.info("renderWithoutSubject");
                final List<Subject> subjects = backend.findSubjectsForUsername(username);
                model.addAttribute("subjects", subjects);
                return "subjectExamsOverview/viewWithoutSubject";
            } else {
                log.info("renderWithSubject subjectId= " + subjectId);
                final List<Exam> examsForSubject = backend.findExamsForSubject(subjectId);
                final List<String> examIdsForUser = backend.findExamIdsForUser(username);
                List<ExamForSubjectExamsView> examsForView = DtoCreator.createDtos(examsForSubject, examIdsForUser, user);
                model.addAttribute("exams", examsForView);
                model.addAttribute("subject", subjectId);
                return "subjectExamsOverview/viewForSubjectWithUser";
            }
        }
    }

    @EventMapping(value = "subject.id")
    private void processShowSubjectEvent(EventRequest eventRequest, PortletSession portletSession) {
        final String subjectId = (String) eventRequest.getEvent().getValue();
        log.info("processShowSubjectEvent " + subjectId);
        portletSession.setAttribute(SessionConstants.SUBJECT_DETAIL, subjectId);
    }

    @RequestMapping(params = "action=show_subject_details")
    private void showSubjectDetails(PortletSession portletSession, @RequestParam(required = true) String
            subjectId, ActionResponse actionResponse, ActionRequest actionRequest) {
        log.info("showSubjectDetails " + subjectId);
        portletSession.setAttribute(SessionConstants.SUBJECT_DETAIL, subjectId);
        AFPortletUtil.redirectToRoot(actionResponse, actionRequest);
    }

    @RequestMapping(params = "action=show_subject_list")
    private void showSubjectList(PortletSession portletSession, ActionResponse actionResponse, ActionRequest actionRequest) {
        log.info("showSubjectList");
        portletSession.removeAttribute(SessionConstants.SUBJECT_DETAIL);
        AFPortletUtil.redirectToRoot(actionResponse, actionRequest);
    }

    @RequestMapping(params = "action=register_exam_action")
    private void registerToExam(@RequestParam(value = "examId") String examId, PortletSession portletSession
            , ActionRequest actionRequest, ActionResponse actionResponse) {
        log.info("registerToExam " + examId);
        final ExamOverviewUser user = (ExamOverviewUser) portletSession.getAttribute(SessionConstants.USER, PortletSession.APPLICATION_SCOPE);
        final String subjectId = (String) portletSession.getAttribute(SessionConstants.SUBJECT_DETAIL);
        user.getExams().put(subjectId, examId);
        AFPortletUtil.redirectToRoot(actionResponse, actionRequest);
    }

    @RequestMapping(params = "action=unregister_exam_action")
    private void unregisterToExam(@RequestParam(value = "examId") String examId, PortletSession portletSession
            , ActionRequest actionRequest, ActionResponse actionResponse) {
        log.info("unregisterToExam " + examId);
        final ExamOverviewUser user = (ExamOverviewUser) portletSession.getAttribute(SessionConstants.USER, PortletSession.APPLICATION_SCOPE);
        final String subjectId = (String) portletSession.getAttribute(SessionConstants.SUBJECT_DETAIL);
        user.getExams().put(subjectId, ExamOverviewUser.NO_EXAM);
        AFPortletUtil.redirectToRoot(actionResponse, actionRequest);
    }


}


